package com.huayue.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.BaseDao;
import com.huayue.dao.MenuDao;
import com.huayue.entity.Menu;
import com.huayue.entity.Role;
import com.huayue.entity.User;

/**
 * 菜单
 */
@Service
public class MenuService extends BaseService<Menu> {

    @Autowired
    MenuDao menuDao;

    @Override
    public BaseDao<Menu> getDao() {
        return menuDao;
    }

    public List<Map<String, Object>> findAllTopMenu() {
        return menuDao.findAllTopMenu();
    }

    public List<Map<String, Object>> findMenuByUser(User user) {
        Role role = new Role();
        role.setId(user.getRole_id());
        List<Map<String, Object>> menuList = menuDao.findAllByRoleId(role);
        List<Map<String, Object>> finalMenu = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : menuList) {
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("title", map.get("name"));
            map1.put("id", map.get("id"));
            map1.put("icon", map.get("icon"));
            map1.put("parentid", map.get("parentid"));
            if (!"".equals(map.get("url"))) {
                map1.put("href", map.get("url"));
            }
            finalMenu.add(map1);
        }
        return finalMenu;
    }
}
